package com.enthusiasm.engine;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Rect;

public class MyBitmap {

	private Bitmap bitmap;

	public MyBitmap(Bitmap bitmap) {
		this.bitmap = bitmap;
	}

	/**
	 * 
	 * @param canvas
	 *            画布
	 * @param bitmap
	 *            源图像
	 * @param dstX
	 *            切片在屏幕显示的坐标X
	 * @param dstY
	 *            切片在屏幕显示的坐标Y
	 * @param flipW
	 *            切片的宽度
	 * @param flipH
	 *            切片的高度
	 * @param srcX
	 *            切片在源图片中的坐标X
	 * @param srcY
	 *            切片在源图片中的坐标Y
	 */
	public void drawImage(Canvas canvas, int dstX, int dstY, int flipW,
			int flipH, int srcX, int srcY) {
		Rect src = new Rect();
		Rect dst = new Rect();

		src.left = srcX;
		src.top = srcY;
		src.right = srcX + flipW;
		src.bottom = srcY + flipH;

		dst.left = dstX;
		dst.top = dstY;
		dst.right = dstX + flipW;
		dst.bottom = dstY + flipH;
		canvas.drawBitmap(bitmap, src, dst, null);
	}

	public void drawImageInRect(Canvas canvas, MyRect rect, int one,int two) {
		float flipW = bitmap.getWidth()/5;
		float flipH = bitmap.getHeight()/2;
		if(one == 0){
			float left = rect.left + rect.width() / 2 - flipW / 2;
			float top = rect.top + rect.height() / 2 - flipH / 2;
			float width = flipW;
			float height = flipH;
			MyRect dst = new MyRect(left, top, width, height);
			
			Rect src = new Rect();
			src.left = (int) (two%5*flipW);
			src.top = (int) (two/5*flipH);
			src.right = (int) (src.left + flipW);
			src.bottom = (int) (src.top + flipH);
			
			canvas.drawBitmap(bitmap, src, dst, null);
		}else{
			float left = rect.left + rect.width() / 2 - flipW;
			float top = rect.top + rect.height() / 2 - flipH / 2;
			float width = flipW;
			float height = flipH;
			MyRect dst_left = new MyRect(left, top, width, height);
			left = rect.left + rect.width() / 2;
			MyRect dst_right = new MyRect(left, top, width, height);
			
			Rect src_left = new Rect();
			src_left.left = (int) (one%5*flipW);
			src_left.top = (int) (one/5*flipH);
			src_left.right = (int) (src_left.left + flipW);
			src_left.bottom = (int) (src_left.top + flipH);
			Rect src_right = new Rect();
			src_right.left = (int) (two%5*flipW);
			src_right.top = (int) (two/5*flipH);
			src_right.right = (int) (src_right.left + flipW);
			src_right.bottom = (int) (src_right.top + flipH);
			
			canvas.drawBitmap(bitmap, src_left,dst_left, null);
			canvas.drawBitmap(bitmap, src_right,dst_right, null);
		}
	}
}
